<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="username">
      <el-input v-model="dataForm.username" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="password">
      <el-input v-model="dataForm.password" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickName">
      <el-input v-model="dataForm.nickName" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="备注信息" prop="note">
      <el-input v-model="dataForm.note" placeholder="备注信息"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="最后登录时间" prop="loginTime">
      <el-input v-model="dataForm.loginTime" placeholder="最后登录时间"></el-input>
    </el-form-item>
    <el-form-item label="帐号启用状态：0->禁用；1->启用" prop="status">
      <el-input v-model="dataForm.status" placeholder="帐号启用状态：0->禁用；1->启用"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          username: '',
          password: '',
          icon: '',
          email: '',
          nickName: '',
          note: '',
          createTime: '',
          loginTime: '',
          status: ''
        },
        dataRule: {
          username: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          nickName: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          note: [
            { required: true, message: '备注信息不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          loginTime: [
            { required: true, message: '最后登录时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '帐号启用状态：0->禁用；1->启用不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/admin/umsadmin/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.umsAdmin.username
                this.dataForm.password = data.umsAdmin.password
                this.dataForm.icon = data.umsAdmin.icon
                this.dataForm.email = data.umsAdmin.email
                this.dataForm.nickName = data.umsAdmin.nickName
                this.dataForm.note = data.umsAdmin.note
                this.dataForm.createTime = data.umsAdmin.createTime
                this.dataForm.loginTime = data.umsAdmin.loginTime
                this.dataForm.status = data.umsAdmin.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/admin/umsadmin/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'icon': this.dataForm.icon,
                'email': this.dataForm.email,
                'nickName': this.dataForm.nickName,
                'note': this.dataForm.note,
                'createTime': this.dataForm.createTime,
                'loginTime': this.dataForm.loginTime,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
